#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

srcdir=filament-d1d873d27f43ba0cee1674a555cc0f18daac3008
mkdir -p ${srcdir}/build || exit 1

extra_args=()
if [ "${arch}" != "${host_arch}" ]; then
    extra_args+=(-DCMAKE_SYSTEM_NAME=Linux) # cross-compiling
    extra_args+=(-DCMAKE_SYSTEM_PROCESSOR=${arch})
    extra_args+=(-DDIST_ARCH=${arch})

    cp -P ${pkgs_root}/${host_dep_filament_isl_fork}/usr/share/cmake/filament/ImportExecutables-Release.cmake ${srcdir}/build/
fi

if [ "${triple_env}" == "ohos" ]; then
    extra_args+=(-DUSE_STATIC_LIBCXX=OFF)
fi

if [ "${arch}" != "${host_arch}" ] && [ ! -f /host/usr/bin/python ]; then
    ln -sf /host/usr/bin/python3 /host/usr/bin/python
fi
if [ "${arch}" == "${host_arch}" ] && [ ! -f /usr/bin/python ]; then
    ln -sf /usr/bin/python3 /usr/bin/python
fi

cmake \
    -S ${srcdir} \
    -B build \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LIBDIR=/usr/lib \
    -DIMPORT_EXECUTABLES_DIR=build \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS} -Wno-error=unqualified-std-cast-call -Wno-error=unused-but-set-variable" \
    ${extra_args[@]} \
    || exit 1
make -C build -j ${MAX_JOBS} || exit 1
